package com.study.controller;

import com.study.pojo.User;
import com.study.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
public class LoginController {
    @Resource
    private UserService userService;
    //登录功能
    @RequestMapping("/login")
    public String login(HttpSession session, String username, String password, Model model){

        User user = userService.find(username,password);

        if (user!=null){
            session.setAttribute("loginUser",user);
            return "redirect:/index.html";
        }else {
            model.addAttribute("msg","用户名或密码错误，请重新输入");
            return "login";
        }
    }


    //注销功能
    @RequestMapping("/user/logout")
    public String loginout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "login";
    }
}
